package com.nondo.rdp.finance.domain.analysis;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.core.domain.base.BaseEntity;
import com.nondo.rdp.finance.config.DbConfig;
import com.nondo.rdp.core.domain.attach.Attach;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.Where;

import javax.persistence.*;

import java.util.List;

/**
 * 金融圈-分析
 */
@Entity
@Table(name = DbConfig.DEFAULT_TABLE_PREFIX + "analysis", indexes = {@Index(columnList = "title")})
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Analysis extends BaseEntity {
    public static final Integer TYPE_GOD = 0;
    public static final Integer TYPE_GENERAL_USER = 1;
    public static final Integer TYPE_INVEST = 2;

    private Long id;    //自增ID
    private String title;   //标题
    private String body;    //内容
    private User user;      //用户信息
    private int type;  //所属类型，1=大咖论世，2=个股分析，3=投资策略
    private Attach mainImg;//封面图片
    private long readNum;// 阅读量
    private long upNum;//点赞数
    private long complaintNum;//投诉次数
    private long commentNum; //评论数量
    private List<AnalysisComment> comment; //所有评论
    // 用于避免重复采集华尔街快讯的id
    private Long wsId;
    private String searchTypeId;//搜索的类型ID
    private int isRead; //是否已经阅读，0=未读，1=已读

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "title", length = 128)
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Column(name = "body", columnDefinition = "text")
    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    @ManyToOne(fetch = FetchType.LAZY) //　---> ManyToOne指定了多对一的关系，fetch=FetchType.LAZY属性表示在多的那一方通过延迟加载的方式加载对象(默认不是延迟加载)
    @JoinColumn(name = "uid") //　--->　　通过 JoinColumn 的name属性指定了外键的名称 uid　(注意：如果我们不通过JoinColum来指定外键的名称，系统会给我们声明一个名称)
    public User getUser() {
        return user;
    }

    @JsonSetter
    public void setUser(User user) {
        this.user = user;
    }

    @Column(name = "type", length = 2)
    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @OneToOne(cascade = CascadeType.ALL)//表示全级联关系
    @JoinColumn(name = "main_img", unique = true)
    public Attach getMainImg() {
        return this.mainImg;
    }

    public void setMainImg(Attach mainImg) {
        this.mainImg = mainImg;
    }

    @Column(columnDefinition = "int default 0", nullable = false)
    public Long getReadNum() {
        return readNum;
    }

    public void setReadNum(Long readNum) {
        this.readNum = readNum;
    }

    @Column(columnDefinition = "int default 0", nullable = false)
    public Long getUpNum() {
        return upNum;
    }

    public void setUpNum(Long upNum) {
        this.upNum = upNum;
    }

    @Column(columnDefinition = "int default 0", nullable = false)
    public Long getComplaintNum() {
        return complaintNum;
    }

    public void setComplaintNum(Long complaintNum) {
        this.complaintNum = complaintNum;
    }

    @Column(columnDefinition = "int default 0", nullable = false)
    public Long getCommentNum() {
        return commentNum;
    }

    public void setCommentNum(Long commentNum) {
        this.commentNum = commentNum;
    }

    @OneToMany(mappedBy = "analysis")
    @LazyCollection(LazyCollectionOption.EXTRA)
    @Where(clause = "del_flag='" + BaseEntity.DEL_FLAG_NORMAL + "'")
    public List<AnalysisComment> getComment() {
        return comment;
    }

    public void setComment(List<AnalysisComment> comment) {
        this.comment = comment;
    }

    @JoinColumn(name = "ws_id", unique = true)
    public Long getWsId() {
        return wsId;
    }

    public void setWsId(Long wsId) {
        this.wsId = wsId;
    }

    @Transient
    public String getSearchTypeId() {
        return searchTypeId;
    }

    public void setSearchTypeId(String searchTypeId) {
        this.searchTypeId = searchTypeId;
    }

    @Transient
    public int getIsRead() {
        return isRead;
    }

    public void setIsRead(int isRead) {
        this.isRead = isRead;
    }
}
